package net.yambal.air.trackManager.GPX
{
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	import com.google.maps.overlays.Polyline;
	
	import mx.collections.ArrayCollection;

    /**
    * GPX.TRK
    * GPX.TRK.TRKSEG のリストが格納された ArrayCollection
    **/
	public class TrackArrayCollection extends ArrayCollection{
		public var name:String;
		
		//public var Segments:Array;
		
		public var SouthLat:Number = 999;
		public var WestLon:Number = 999;
		public var NorthLat:Number = -999;
		public var EastLon:Number = -999;
		
		public function TrackArrayCollection(trk:XML){
			trk.ignoreWhitespace = true;
			var ns:Namespace = trk.namespace();
            
			var segmentsXMLList:XMLList = trk.ns::trkseg;
			
			this.name = trk.ns::name;
            
			for(var i:Number = 0; i < segmentsXMLList.length(); i++){
                var segmentXML:XML = segmentsXMLList[i] as XML;
				var segment:WayPointArrayCollection = new WayPointArrayCollection(segmentXML, WayPointArrayCollection.LIST_TYPE_TRK);

                this.addItem(segment);
				
				if(segment.SouthLat < this.SouthLat){
					this.SouthLat = segment.SouthLat;
				}
				if(segment.NorthLat > this.NorthLat){
					this.NorthLat = segment.NorthLat;
				}
				if(segment.WestLon < this.WestLon){
					this.WestLon = segment.WestLon;
				}
				if(segment.EastLon > this.EastLon){
					this.EastLon = segment.EastLon;
				}
			}
		}
        
        public function get gPolylines():Array{
            var res:Array = new Array();
            for each(var seg:WayPointArrayCollection in this){
                res.push(seg.gPolyline);
            }
            return res;
        }

		public function get bounds():LatLngBounds{
			var southWest:LatLng = new LatLng(this.SouthLat, this.WestLon);
			var northEast:LatLng = new LatLng(this.NorthLat, this.EastLon);
			return new LatLngBounds(southWest,northEast);
		}
		
		public function get center():LatLng{
			return new LatLng((this.SouthLat + this.NorthLat) / 2, (this.WestLon + this.EastLon) / 2);
		}
	}
}